﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ModelLayer;
// services
using ScheduleCtrlService;
using SeatCtrlService;
using ReservationCtrlService;
using System.Collections.Specialized;
using System.Text;
using System.IO;

public partial class seats : System.Web.UI.Page
{

	protected void Page_Load(object sender, EventArgs e)
	{
	}

	public string GetTable()
	{
		NameValueCollection GET = Request.QueryString;
		int selected = Int32.Parse(GET.GetValues("sched")[0]);
		if (selected == 0)
			return "";

		Table table = new Table();

		SeatCtrlClient seatService = new SeatCtrlClient();
		ScheduleCtrlClient scheduleService = new ScheduleCtrlClient();

		Seat[] takenSeats = seatService.getTakenSeats(selected);
		Schedule schedule = scheduleService.getScheduleById(selected);
		Seat[] roomSeats = seatService.getSeatsByRoom(schedule.room.id);

		CreateSeatTable(seatService, schedule.room.id, table);
		foreach (Seat seat in roomSeats)
		{
			string css = "seatFree";
			ReservationCtrlClient reservationService = new ReservationCtrlClient();
			Reservation[] reservations = reservationService.getReservations();
			reservationService.Close();
			foreach (Seat s in takenSeats)
				if (s.id == seat.id)
				{
					if (InReservation(reservations, s.id))
						css = "seatIdle";
					else
						css = "seatReserved";
					break;
				}

			table.Rows[seat.row - 1].Cells[seat.col - 1].CssClass = css;
			table.Rows[seat.row - 1].Cells[seat.col - 1].Text += "<input type='hidden' name='seat-id' value='" + seat.id + "' />";
		}

		if (table.Rows.Count > 0 && table.Rows[0].Cells.Count > 0)
			table.Rows[0].Cells[0].Text += "<input id='schedule-id' type='hidden' name='schedule-id' value='" + schedule.id + "' />";

		AddSeatClickListeners(table);

		return RenderControl(table);
	}

	protected void AddSeatClickListeners(Table table)
	{
		for (int i = 0; i < table.Rows.Count; ++i)
		{
			for (int j = 0; j < table.Rows[i].Cells.Count; ++j)
			{
				table.Rows[i].Cells[j].Attributes.Add("onClick", "SeatClicked(this);");
			}
		}
	}

	private bool InReservation(Reservation[] reservations, int seatId)
	{
		foreach (Reservation r in reservations)
			if (r.Ticket.seat.id == seatId)
				return true;
		return false;
	}

	private void CreateSeatTable(SeatCtrlClient seatService, int roomId, Table table)
	{
		int row = seatService.getRowCount(roomId);
		int rowNr = 0;
		for (int i = 0; i < row; i++)
		{
			TableRow r = new TableRow();
			int col = seatService.getColCountInRow(roomId, i + 1);
			for (int j = 0; j < col; j++)
			{
				TableCell cell = new TableCell();
				cell.Text = (++rowNr).ToString();
				r.Cells.Add(cell);
			}
			table.Rows.Add(r);
		}
	}

	private string RenderControl(Control ctrl)
	{
		StringBuilder sb = new StringBuilder();
		StringWriter tw = new StringWriter(sb);
		HtmlTextWriter hw = new HtmlTextWriter(tw);

		ctrl.RenderControl(hw);
		return sb.ToString();
	}
}